/**
 * DbAdapter
 * 
 * 05/12/2013
 * 
 * Copyright GPL
 */
package com.jtmproject.javiertejedor.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * this is a class to manipulate the database
 * @author Javier Tejedor
 * @Thanks Pello Altadill
 */
public class DbAdapter {

	private SQLiteDatabase db;
	private SqLiteHelper dbHelper;
	private final Context context;

	/**
	 * constructor
	 * @param context
	 */
	public DbAdapter(Context context) {
		this.context = context;
	}

	/**
	 * this opens the database
	 * @return
	 * @throws SQLException
	 */
	public SQLiteDatabase open() throws SQLException {
		dbHelper = new SqLiteHelper(context);
		db = dbHelper.getWritableDatabase();
		return db;
	}

	/**
	 * this closes the database
	 */
	public void close() {
		dbHelper.close();
	}

	/**
	 * this inserts a secret
	 * @param secret
	 * @return
	 */
	public long insertSecret(String secret) {
		ContentValues registro = new ContentValues();

		registro.put("secret", secret);

		return db.insert("secrets", null, registro);
	}

	/**
	 * this gets all secrets
	 * @return
	 */
	public Cursor getAllSecrets() {
		return db.query("secrets", new String[] {"_id","secret"}, null, null, null, null, null);
	}
	
}